package com.hqyj.paimaisys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hqyj.paimaisys.model.Auction;
import com.hqyj.paimaisys.model.Dealrecord;
import com.hqyj.paimaisys.model.Huiyuan;
import com.hqyj.paimaisys.service.AuctionService;
import com.hqyj.paimaisys.service.DealrecordService;
import com.hqyj.paimaisys.service.HuiyuanService;
import com.hqyj.paimaisys.web.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-10-20
 */
@RestController
@RequestMapping("/dealrecord")
public class DealrecordController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    DealrecordService dealrecordService;

    @Autowired
    AuctionService auctionService;

    @Autowired
    HuiyuanService huiyuanService;



    @RequestMapping("/myorder")
    public ResponseResult<List<Dealrecord>> myorder(HttpServletRequest request){
          String token = request.getHeader("token");
          String hid = redisTemplate.opsForValue().get(token);

          QueryWrapper<Dealrecord> wrapper = new QueryWrapper<Dealrecord>();
          wrapper.eq("hid", hid);

          List<Dealrecord> list = dealrecordService.list(wrapper);
          list.forEach(dealrecord -> {
              Auction auc = auctionService.getById(dealrecord.getAid());
              dealrecord.setGpic(auc.getGpic());
          });

          return ResponseResult.success(list);
    }

    @RequestMapping("/fukuan")
    public ResponseResult<String> fukuan(Integer cjid, HttpServletRequest request) {
        //判断余额够不够
        String token = request.getHeader("token");
        String hid = redisTemplate.opsForValue().get(token);
        Huiyuan hy = huiyuanService.getById(hid);

        Dealrecord dealrecord = dealrecordService.getById(cjid);
        Auction auction = auctionService.getById(dealrecord.getAid());

        //会员的可用资金
        int hymoney = hy.getHyue() + auction.getAbmoney();

        //商品的成交金额
        int cjmoney = dealrecord.getCjmoney();
        if(hymoney < cjmoney){
            return ResponseResult.error("钱不够，先充值");
        }

        // 修改余额  update huiyuan set hicemoney=hicemoney-保证金, hyue=hyue+保证金-cjmoney where hid=?
        UpdateWrapper<Huiyuan> updateWrapper = new UpdateWrapper<Huiyuan>();
        updateWrapper.set("hicemoney",hy.getHicemoney()-auction.getAbmoney());
        updateWrapper.set("hyue",hy.getHyue()+auction.getAbmoney()-cjmoney);
        updateWrapper.eq("hid",hid);

        // 修改交易记录  update dealrecord set did = 2, cjtime=? where cjid=?
        UpdateWrapper<Dealrecord> updateWrapper2 = new UpdateWrapper<Dealrecord>();
        updateWrapper2.set("did", Dealrecord.STATUS_OK);
        updateWrapper2.eq("cjid", cjid);
        dealrecordService.fukuan(updateWrapper,updateWrapper2);
        return ResponseResult.success("付款成功");
    }


}

